package com.qf.erp_system_admin.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.erp_system_admin.entity.Client;
import com.qf.erp_system_admin.entity.ClientVo;
import com.qf.erp_system_admin.service.FinanceService;
import com.qf.erp_system_admin.util.R;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/finance")
public class FinanceContoroller {
    private Logger logger = LogManager.getLogger(FinanceContoroller.class);
    @Autowired
    private FinanceService financeService;
    @GetMapping("/twoList")
    public R twoList(@RequestParam HashMap<String,String> map) {
        int pageNum = Integer.parseInt(map.get("pageNum"));
        int pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(pageNum,pageSize);
        List<ClientVo> clients = financeService.twoList(map);
        PageInfo<ClientVo> info = new PageInfo<>(clients);
        System.out.println("info = " + info);
        return  R.ok(info);
    }
    @GetMapping("/find3")
    public R find3() {
        List<String> list =  financeService.find3();
        return R.ok(list);
    }
//    @GetMapping("/find4")
//    public R find4() {
//        List<String> list =  financeService.find4();
//        return R.ok(list);
//    }
@GetMapping("/oneList")
public R oneList() {
    List<ClientVo> list = financeService.oneList();
    return  R.ok(list);
}
    /**
     * 下载Excel文件
     * 注意,返回值是void
     */
    @GetMapping("/excel")
    public void downloadExcel(HttpServletResponse response) throws IOException {

        //  查询数据库全部数据
        List<ClientVo> list = financeService.findAll(null);
        System.out.println("list = " + list);

        // 导出.xls用这个
        // response.setContentType("application/vnd.ms-excel");
        // 导出.xlsx用这个
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("财务回款信息表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // easyexcel导出工具
        EasyExcel.write(response.getOutputStream(), ClientVo.class).autoCloseStream(Boolean.FALSE).sheet("财务回款信息表")
                .doWrite(list);

    }

}
